package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dto.account.OrderAutoAccountResultDTO;
import com.zmn.oms.zmn.business.interfaces.account.OrderAutoAccountResultBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 描述：算账结果消息处理
 *
 * @author xiewenbing
 * @since 2022/8/18 9:19
 */
@Slf4j
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
public class OrderAutoAccountResultListener {

    @Autowired
    private OrderAutoAccountResultBService orderAutoAccountResultBService;

    @AliyunMQHandler(tags = OmsMqTagConsts.ORDER_AUTO_ACCOUNT_RESLUT_TAG)
    public void orderAccoutResult(String message) {

        log.debug("#mq#orderAccoutResult# message:{}", message);

        try {
            if (StringUtil.isBlank(message)) {
                return;
            }

            OrderAutoAccountResultDTO orderAutoAccountResultDTO = JSON.parseObject(message, OrderAutoAccountResultDTO.class);
            if (Objects.isNull(orderAutoAccountResultDTO)) {
                return;
            }

            orderAutoAccountResultDTO.setChecksum(DigestUtils.md5Hex(message));
            orderAutoAccountResultBService.saveOrderAutoAccountResult(orderAutoAccountResultDTO);

            log.debug("#mq#orderAccoutResult# success");
        } catch (Exception e) {
            log.error("#mq#orderAccoutResult# exception：[{}][{}]", e.getMessage(), e);
        }
    }
}
